package cn.jishiyu11.xeqb.base

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View

//import com.gyf.barlibrary.ImmersionBar

import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.activity.CommissioningActivity
import cn.jishiyu11.xeqb.utils.NetWorkUtils
import cn.jishiyu11.xeqb.utils.showShortToast
import com.gyf.barlibrary.ImmersionBar

/**
 * 基础类  BaseAvtivity
 */
@SuppressLint("NewApi")
abstract class BaseActivity : AppCompatActivity() {//implements OnClickRefreshListener, OnPowerKeyListener {

    private var time: Long = -1

    private var receiver: Receiver? = null

    private var mImmersionBar: ImmersionBar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mImmersionBar = ImmersionBar.with(this)
        mImmersionBar!!.init()
        val window = window
        val params = window.attributes
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window.attributes = params
        BaseActivityManager.activityManager.pushActivity(this)

    }


//    override fun setContentView(layoutResID: Int) {
//        super.setContentView(layoutResID)
//    }
//
//    override fun setContentView(view: View) {
//        super.setContentView(view)
//    }
//
//    override fun setContentView(view: View, params: LayoutParams) {
//        super.setContentView(view, params)
//    }

    /**
     * 广播
     */
    private inner class Receiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {}
    }

    override fun onDestroy() {
        super.onDestroy()
        if (receiver != null) {
            unregisterReceiver(receiver)
            receiver = null
        }

        if (mImmersionBar != null) {
            mImmersionBar!!.destroy()
        }
    }

    @SuppressLint("RestrictedApi")
    override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) {
        if (System.currentTimeMillis() - time < 1000) {
            return
        }
        time = System.currentTimeMillis()
        IsClass(intent)
        super.startActivityForResult(intent, requestCode, options)
    }

    override fun onSaveInstanceState(arg0: Bundle) {
        super.onSaveInstanceState(arg0)
        val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val _pkgName = activityManager.getRunningTasks(1)[0].topActivity.packageName
    }

    /**
     * 绑定控件id
     */
    protected abstract fun findViewById()

    /**
     * 初始化控件
     */
    protected abstract fun initView()

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     */
    protected fun openActivity(pClass: Class<*>, pBundle: Bundle?) {
        val intent = Intent(this, pClass)
        if (pBundle != null) {
            intent.putExtras(pBundle)
        }
        startActivity(intent)
    }

    /**
     * 通过Action启动Activity，并且含有Bundle数据
     */
    protected fun openActivity(pAction: String, pBundle: Bundle?) {
        val intent = Intent(pAction)
        if (pBundle != null) {
            intent.putExtras(pBundle)
        }
        startActivity(intent)
    }

    fun NetWorkshow(intent: Intent): Boolean {
        if (intent.component != null) {
        }
        return false
    }

    fun IsClass(intent: Intent) {
        if (NetWorkshow(intent)) {
        } else {
            if (!NetWorkUtils.isNetworkConnected(this) && intent.component != null
                    && intent.component!!.className != CommissioningActivity::class.java.name) {
                this.showShortToast(getString(R.string.no_network_timed))

            }
        }
    }
}
